package com.wxq.workpad.utils.autosize;

import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.content.res.Resources;
import com.wxq.workpad.utils.ApplicationUtils;
import com.wxq.workpad.utils.DeviceUtils;
import me.jessyan.autosize.AutoSizeConfig;
/**
 * @author ArcherYc
 * @date on 2020/4/24  14:46
 * @mail 247067345@qq.com
 */
public class AutoSizeHelper {

    /**
     * 始终使用竖屏的宽度来适配。防止旋转过后，屏幕中的图标文字过大。
     *
     * @param activity
     */
    public static void resetScreenSize(Activity activity) {
        if (activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            int width = DeviceUtils.getScreenWidth(activity);
            if (width > 0) {
                AutoSizeConfig.getInstance().setScreenWidth(width);
            }
        } else {
            int height = DeviceUtils.getScreenHeight(activity);
            if (height > 0) {
                AutoSizeConfig.getInstance().setScreenWidth(height);
            }
        }
    }

    public static void judgeAndFitWidth() {
//        if (DeviceUtils.isFoldDevice(ApplicationUtils.getApp())) {
//            float heightWidthRatio = DeviceUtils.screenHeight(ApplicationUtils.getApp()) * 1.0f /
//                    DeviceUtils.screenWidth(ApplicationUtils.getApp());
//            int targetWidth = (int) (685 / heightWidthRatio);
//            AutoSizeConfig.getInstance().setDesignWidthInDp(targetWidth);
//        } else {
            AutoSizeConfig.getInstance().setDesignWidthInDp(375);
//        }
    }

    /**
     * 用于Dialog，防止宽高异常
     *
     * @param context
     * @return
     */
    public static Context adjustAutoSize(Context context) {
        return new ContextWrapper(context) {
            private Resources mResources;

            {
                Resources oldResources = super.getResources();
                mResources = new Resources(oldResources.getAssets(), oldResources.getDisplayMetrics(), oldResources.getConfiguration());
            }

            @Override
            public Resources getResources() {
                mResources.getDisplayMetrics().density = AutoSizeConfig.getInstance().getInitDensity();
                return mResources;
            }
        };
    }
}
